using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PlanetSim
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D particle;
        ParticleSimulator.Simulation simulation;
        const float simSpeed = 1f;

        MouseState mouseState;
        MouseState lastMouseState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Components.Add(new FrameRateCounter(this));
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            ParticleSimulator.SimulationParameters parameters = ParticleSimulator.SimulationParameters.BuildDefault(GraphicsDevice.PresentationParameters.Bounds.Width, GraphicsDevice.PresentationParameters.Bounds.Height);
            simulation = new ParticleSimulator.Simulation(parameters);
            Random random = new Random();
            for (int i = 0; i < 250; i++)
            {
                simulation.AddParticle(new ParticleSimulator.ParticleData(random.Next((int)simulation.Width), random.Next((int)simulation.Height), 1f));
            }

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            particle = Content.Load<Texture2D>("particle");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            //simulation.Simulate(simSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
            simulation.Simulate(.1f);
            if (mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released) simulation.AddParticle(new ParticleSimulator.ParticleData(Mouse.GetState().X, Mouse.GetState().Y, 1f));

            lastMouseState = mouseState;
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            List<ParticleSimulator.ParticleData> data = simulation.GetParticleData();

            spriteBatch.Begin();

            foreach (ParticleSimulator.ParticleData p in data)
            {
                float velocity = (float)Math.Sqrt(Math.Abs(p.VelocityX) + Math.Abs(p.VelocityY)) / 8f;
                Color color = new Color(p.Density * 1000, 0, 0);
                color = Color.Navy;
                spriteBatch.Draw(particle, new Vector2(p.X, p.Y), null, color, 0f, new Vector2(32, 32), p.Radius / 32f, SpriteEffects.None, 0f);
                //spriteBatch.Draw(particle, new Vector2(p.X, p.Y + simulation.Height), null, color, 0f, new Vector2(32, 32), p.Radius / 32f, SpriteEffects.None, 0f);
                //spriteBatch.Draw(particle, new Vector2(p.X, p.Y - simulation.Height), null, color, 0f, new Vector2(32, 32), p.Radius / 32f, SpriteEffects.None, 0f);
                //spriteBatch.Draw(particle, new Vector2(p.X + simulation.Width, p.Y), null, color, 0f, new Vector2(32, 32), p.Radius / 32f, SpriteEffects.None, 0f);
                //spriteBatch.Draw(particle, new Vector2(p.X - simulation.Width, p.Y), null, color, 0f, new Vector2(32, 32), p.Radius / 32f, SpriteEffects.None, 0f);
            }

            spriteBatch.Draw(particle, new Vector2(mouseState.X, mouseState.Y), null, Color.DarkRed * .5f, 0f, new Vector2(32, 32), 16 / 32f, SpriteEffects.None, 0f);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
